package com.linxianqin.springbootVue.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.linxianqin.springbootVue.controller.system.SysDictDataController;
import com.linxianqin.springbootVue.domain.convert.SysDictDataConvert;
import com.linxianqin.springbootVue.domain.convert.SysRoleConvert;
import com.linxianqin.springbootVue.domain.dto.SysDictDataDTO;
import com.linxianqin.springbootVue.domain.entity.SysDictData;
import com.linxianqin.springbootVue.domain.entity.SysRole;
import com.linxianqin.springbootVue.domain.query.SysDictDataQuery;
import com.linxianqin.springbootVue.mapper.SysDictDataMapper;
import com.linxianqin.springbootVue.service.SysDictDataService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.linxianqin.springbootVue.utils.DateUtils;
import com.linxianqin.springbootVue.utils.DictUtils;
import com.linxianqin.springbootVue.utils.text.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 字典数据表 服务实现类
 * </p>
 *
 * @author lxq
 * @since 2020-12-14
 */
@Service
public class SysDictDataServiceImpl extends ServiceImpl<SysDictDataMapper, SysDictData> implements SysDictDataService {
    @Override
    public List<SysDictData> selectDictDataByType(String dictType) {
        List<SysDictData> dictDatas = DictUtils.getDictCache(dictType);
        if (StringUtils.isNotEmpty(dictDatas))
        {
            return dictDatas;
        }
        LambdaQueryWrapper<SysDictData> queryWrapper = new QueryWrapper<SysDictData>().lambda();
        queryWrapper.eq(SysDictData::getStatus,"0")
                .eq(SysDictData::getDictType,dictType)
                .orderByAsc(SysDictData::getDictSort);
        dictDatas = baseMapper.selectList(queryWrapper);
        if (StringUtils.isNotEmpty(dictDatas))
        {
            DictUtils.setDictCache(dictType, dictDatas);
            return dictDatas;
        }
        return null;
    }
    /**
     * 根据条件分页查询字典数据
     * @param page 分页信息
     * @param query 字典数据信息
     * @return 字典数据集合信息
     */
    @Override
    public Page<SysDictDataDTO> selectSysDictDataPage(Page<SysDictDataDTO> page, SysDictDataQuery query) {
        Page<SysDictData> entityPage = SysDictDataConvert.INSTANCE.dto2entity(page);
        LambdaQueryWrapper<SysDictData> queryWrapper = new QueryWrapper<SysDictData>().lambda();
        if (StringUtils.isNotNull(query.getStatus())){
            queryWrapper.eq(SysDictData::getStatus,query.getStatus());
        }
        if (StringUtils.isNotNull(query.getDictLabel())){
            queryWrapper.like(SysDictData::getDictLabel,query.getDictLabel());
        }
        if (StringUtils.isNotNull(query.getDictType())){
            queryWrapper.eq(SysDictData::getDictType,query.getDictType());
        }
        if (StringUtils.isNotNull(query.getParams())){
            Map<String, Object> params = query.getParams();
            Object beginTime = params.get("beginTime");
            if (beginTime!=null){
                queryWrapper.ge(SysDictData::getCreateTime,beginTime);
            }
            Object endTime = params.get("endTime");
            if (beginTime!=null){
                endTime = DateUtils.addOneDay(DateUtils.dateTime("yyyy-MM-dd", endTime.toString()));
                queryWrapper.ge(SysDictData::getCreateTime,endTime);
            }
        }
        Page<SysDictData> selectPage = baseMapper.selectPage(entityPage, queryWrapper);
        return SysDictDataConvert.INSTANCE.entity2dto(selectPage);
    }
}
